﻿using Microsoft.AspNetCore.Mvc;
using RuoVea.OmiApi.UserRole.Entitys;
using RuoVea.OmiApi.UserRole.Service;
using RuoVea.OmiApi.UserRole.Service.Dto;
using RuoVea.OmiUserRole.Attributes;

namespace RuoVea.OmiUserRole.Controllers;
/// <summary>
/// 
/// </summary>
public class PageRoleInputEx : PageRoleInput
{
    private int _currentPage;
    /// <summary>
    /// 
    /// </summary>
    public int CurrentPage
    {
        get { return _currentPage; }
        set
        {
            _currentPage = value;
            base.PageNo = value;
        }
    }
}
/// <summary>
/// 角色 
/// </summary> 
[AppRoute]
public class RoleController : Controller
{
    private readonly SysRoleService _sysRoleService;
    private readonly SysUserRoleService _sysUserRoleService;

    /// <summary>
    /// 
    /// </summary>
    /// <param name="sysRoleService"></param>
    /// <param name="sysUserRoleService"></param>

    public RoleController(SysRoleService sysRoleService, SysUserRoleService sysUserRoleService)
    {
        _sysRoleService = sysRoleService;
        _sysUserRoleService = sysUserRoleService;
    }

    /// <summary>
    /// 
    /// </summary>
    /// <returns></returns>
    [HttpGet(nameof(Index))]
    public IActionResult Index() { return View(); }

    /// <summary>
    /// 
    /// </summary>
    /// <returns></returns>
    [HttpGet("roleuser/{id:long}")]
    public IActionResult RoleUser(long id) { ViewBag.Id = id; return View(); }

    /// <summary>
    /// 分页获取角色列表
    /// </summary>
    /// <param name="data"></param>
    /// <returns></returns>
    [HttpPost("pages")]
    public async Task<RestfulResult<PageResult<SysRole>>> GetPagesAsync([FromBody] PageRoleInputEx data)
    {
        RestfulResult<PageResult<SysRole>> restfulResult = new() { Code = CodeStatus.OK };
        restfulResult.Data = await _sysRoleService.Page(data);
        return restfulResult;
    }

    /// <summary>
    /// 增加
    /// </summary>
    /// <param name="data"></param>
    /// <returns></returns>
    [UnitOfWork]
    [HttpPost("add")]
    public async Task<RestfulResult> AddAsync(AddRoleInput data)
    {
        RestfulResult restfulResult = new() { Code = CodeStatus.OK, Data = true };
        try
        {
            await _sysRoleService.AddRole(data);
            return restfulResult;
        }
        catch (Exception ex)
        {
            restfulResult.Code = CodeStatus.BadRequest;
            restfulResult.Data = false;
            restfulResult.Message = ex.Message;
            return restfulResult;
        }
    }
    /// <summary>
    /// 更新
    /// </summary>
    /// <param name="data"></param>
    /// <returns></returns>
    [HttpPost("update")]
    public async Task<RestfulResult> UpdateAsync(AddRoleInput data)
    {
        RestfulResult restfulResult = new() { Code = CodeStatus.OK, Data = true };
        try
        {
            await _sysRoleService.UpdateRole(data);
            return restfulResult;
        }
        catch (Exception ex)
        {
            restfulResult.Code = CodeStatus.BadRequest;
            restfulResult.Data = false;
            restfulResult.Message = ex.Message;
            return restfulResult;
        }
    }

    /// <summary>
    /// 获取角色相关信息
    /// </summary>
    /// <returns></returns>
    [HttpGet(nameof(GetRoleList))]
    public async Task<RestfulResult<List<RoleOutput>>> GetRoleList()
    {
        RestfulResult<List<RoleOutput>> restful = new() { Code= CodeStatus.OK };
        restful.Data= await _sysRoleService.GetList();
        return restful;
    }

    /// <summary>
    /// 获取用户角色相关信息（登录）
    /// </summary>
    /// <param name="userId"></param>
    /// <returns></returns>
    [HttpGet("userrolelist/{userId:long}")]
    public async Task<RestfulResult<List<SysRole>>> GetUserRoleList(long userId)
    {
        RestfulResult<List<SysRole>> restful = new() { Code = CodeStatus.OK };
        restful.Data = await _sysUserRoleService.GetUserRoleList(userId);
        return restful;
    }

    /// <summary>
    /// 获取用户下的角色没有就返回所有角色（用于授权角色时选择）
    /// </summary>
    /// <returns></returns>
    [HttpGet("role/dropDown/{userId:long}")]
    public async Task<List<SysRole>> GetRoleDropDown(long userId)
    {
        return await _sysUserRoleService.GetUserRoleList(userId);
    }

    /// <summary>
    /// 删除角色
    /// </summary>
    /// <param name="data"></param>
    /// <returns></returns>
    [HttpDelete("delete")]
    public async Task<RestfulResult> DeleteAsync(DeleteRoleInput data)
    {
        RestfulResult restfulResult = new() { Code = CodeStatus.OK, Data = true };
        try
        {
            await _sysRoleService.DeleteRole(data);
            return restfulResult;
        }
        catch (Exception ex)
        {
            restfulResult.Code = CodeStatus.BadRequest;
            restfulResult.Data = false;
            restfulResult.Message = ex.Message;
            return restfulResult;
        }
    }

    /// <summary>
    /// 修改状态
    /// </summary>
    /// <param name="data"></param>
    /// <returns></returns>
    [HttpPost(nameof(ChangeStatus))]
    public async Task<RestfulResult> ChangeStatus([FromBody] RoleInput data)
    {
        RestfulResult restfulResult = new() { Code = CodeStatus.OK, Data = true };
        try
        {
            long result = await _sysRoleService.SetStatus(data);
            if (result > 0) return restfulResult;
            else
            {
                restfulResult.Code = CodeStatus.BadRequest;
                restfulResult.Data = false;
                return restfulResult;
            }
        }
        catch (Exception ex)
        {
            restfulResult.Code = CodeStatus.BadRequest;
            restfulResult.Data = false;
            restfulResult.Message = ex.Message;
            return restfulResult;
        }
    }
    /// <summary>
    /// 授权角色用户
    /// </summary>
    /// <param name="data">用户Id列表</param>
    /// <returns></returns>
    [HttpPost("grantroleusers")]
    public async Task<RestfulResult> GrantRoleUsers(RoleUsersInput data)
    {
        RestfulResult restful = new() { Code = CodeStatus.BadRequest };
        try
        {
            await _sysUserRoleService.GrantUsersRole(data);
            restful.Code = CodeStatus.OK;
            restful.Data = true;
            return restful;
        }
        catch (Exception ex)
        {
            restful.Message = ex.Message;
            return restful;
        }
    }
    /// <summary>
    /// 移除角色的用户
    /// </summary>
    /// <param name="data">用户Id列表</param>
    /// <returns></returns>
    [HttpPost("removeroleusers")]
    public async Task<RestfulResult> RemoveRoleUsers(RoleUsersInput data)
    {
        RestfulResult restful = new() { Code = CodeStatus.BadRequest };
        try
        {
            await _sysUserRoleService.DeleteUserRoleByRoleIdUserIds(data);
            restful.Code = CodeStatus.OK;
            restful.Data = true;
            return restful;
        }
        catch (Exception ex)
        {
            restful.Message = ex.Message;
            return restful;
        }
    }
}

